package com.innovate.nms.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.springframework.stereotype.Component;

import com.ivan.toolkit.swing.ShadowLabel;
import com.ivan.toolkit.swing.UnderlineButton;

@Component
public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3630571891109435766L;
	
	
	private JPanel contentPane;
	private ShadowLabel shadowLabel;
	private JPanel panelCenter;
	private UnderlineButton underlineButton;
	private DebugPanel debugPanel;
	private MonitorPanel monitorPanel;
	
	private boolean debug = false;

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/app.png")));
		setExtendedState(Frame.MAXIMIZED_BOTH);
		//this.setIconImage(Application.getIconImage());
		setTitle("网络监测软件 V2.0");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 678, 450);
		contentPane = new JPanel();
		contentPane.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		contentPane.setBorder(null);
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panelTitle = new JPanel();
		panelTitle.setBorder(new EmptyBorder(0, 10, 0, 10));
		panelTitle.setBackground(new Color(119, 136, 153));
		panelTitle.setPreferredSize(new Dimension(10, 50));
		contentPane.add(panelTitle, BorderLayout.NORTH);
		GridBagLayout gbl_panelTitle = new GridBagLayout();
		gbl_panelTitle.columnWidths = new int[]{144, 0, 0, 0, 0};
		gbl_panelTitle.rowHeights = new int[]{33, 0};
		gbl_panelTitle.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panelTitle.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panelTitle.setLayout(gbl_panelTitle);
		
		shadowLabel = new ShadowLabel();
		shadowLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		shadowLabel.setForeground(Color.WHITE);
		shadowLabel.setText("网络监测软件");
		GridBagConstraints gbc_shadowLabel = new GridBagConstraints();
		gbc_shadowLabel.insets = new Insets(0, 0, 0, 5);
		gbc_shadowLabel.anchor = GridBagConstraints.WEST;
		gbc_shadowLabel.gridx = 0;
		gbc_shadowLabel.gridy = 0;
		panelTitle.add(shadowLabel, gbc_shadowLabel);
		
		underlineButton = new UnderlineButton();
		underlineButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showDebug(!debug);
			}
		});
		underlineButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		underlineButton.setBackground(Color.LIGHT_GRAY);
		underlineButton.setForeground(Color.WHITE);
		underlineButton.setText("\u8FDB\u5165\u7BA1\u7406\u6A21\u5F0F");
		GridBagConstraints gbc_underlineButton = new GridBagConstraints();
		gbc_underlineButton.gridx = 3;
		gbc_underlineButton.gridy = 0;
		panelTitle.add(underlineButton, gbc_underlineButton);
		
		panelCenter = new JPanel();
		contentPane.add(panelCenter, BorderLayout.CENTER);
		panelCenter.setLayout(new CardLayout(0, 0));
		
		monitorPanel = new MonitorPanel();
		panelCenter.add(monitorPanel, "monitor");
		
		debugPanel = new DebugPanel();
		panelCenter.add(debugPanel, "debug");
		
		showDebug(false);
		
	}
	
	private void showDebug(boolean debug) {
		CardLayout layout = (CardLayout)panelCenter.getLayout();
		layout.show(panelCenter, debug ? "debug" : "monitor");
		underlineButton.setText(debug ? "退出调试模式" : "进入调试模式");
		this.debug = debug;
	}
	
	
	@Override
	protected void processWindowEvent(WindowEvent e) {
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			int ret = JOptionPane.showConfirmDialog(null, "确定要退出系统？", "退出",
					JOptionPane.YES_NO_OPTION);
			if(ret == JOptionPane.YES_OPTION) {
				super.processWindowEvent(e);
			}
		}
	}

	/*
	public void changeMode() {
		CardLayout layout = (CardLayout)panelCenter.getLayout();
		if(!manageMode) {
			
			SystemMgrPswdVerifier v = new SystemMgrPswdVerifier();
			v.setAlwaysOnTop(true);
			v.start(this);
			if(!v.isSucceed()) {
				return;
			}
			
			layout.last(panelCenter);
			underlineButton.setText("退出管理模式");
			manageMode = true;
		} else {
			layout.first(panelCenter);
			underlineButton.setText("进入管理模式");
			manageMode = false;
		}
	}
	*/

}
